package com.tianqu.tms.www.settle.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TmsTransferFinanceDetailDao;
import com.tianqu.tms.dao.TmsTransferFinanceDetailDaoImp;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferSettlementDate;
import com.tianqu.tms.entity.TransferSettlementInfo;
import com.tianqu.tms.www.settle.viewhelper.TransferSettleViewHelper;

public class TransferSettleReSettleController extends TmsController {
	private Logger logger = Logger.getLogger(TransferSettleReSettleController.class);
	
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		// 添加事务提交异常捕获
		try {
			controller.begin();
			
			TransferSettleViewHelper view = (TransferSettleViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			String insertUserId = (String)uc.getAttribute("settleInsertUserId");
			//根据结算日期ID getSettleId删除每日结算表信息
			String settleID = view.getSettleId();
			//根据userId和settleId获取唯一settleId
			TransferSettlementDate settleDate =new TransferSettlementDate();
			settleDate = settleDate.getDateInfoBySettleId(view.getSettleId(),Integer.parseInt(insertUserId));
			settleID = String.valueOf(settleDate.getId());
//			if(settleID.indexOf(")")>-1)
//			{
//				settleID = view.getSettleId().substring(1, view.getSettleId().length()-1);
//			}
			TransferSettlementInfo reSettle = new TransferSettlementInfo();
			reSettle.deleteSettleBySettleId(Integer.parseInt(settleID),Integer.parseInt(insertUserId));
			
//			//流程表收费 查询出时间断内的所有人
//			TransferFinanceInfo finance = new TransferFinanceInfo();
//			List<TransferFinanceInfo> lcList = finance.getFinaneInfoByPeriodLC(view.getStartDate(),view.getEndDate(),Integer.parseInt(insertUserId));
//			if(lcList!=null)
//			{
//				for(int i=0;i<lcList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)lcList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(settleID));
//					settleInfo.setSettleUserId(Integer.parseInt(insertUserId));
//					settleInfo.setReceivable(50*entity.getCount());
//					settleInfo.setPaid(entity.getCheckPrice());
//					settleInfo.setInsertUserId(entity.getCheckPriceUserId());
//					settleInfo.setSettletype(1);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//
//			//交易票收费
//			List<TransferFinanceInfo> jypList = finance.getFinaneInfoByPeriodJYP(view.getStartDate(),view.getEndDate(),Integer.parseInt(insertUserId));
//			if(jypList!=null)
//			{
//				for(int i=0;i<jypList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)jypList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(settleID));
//					settleInfo.setSettleUserId(Integer.parseInt(insertUserId));
//					settleInfo.setReceivable(200*entity.getCount());
//					settleInfo.setPaid(entity.getTransferPrice());
//					settleInfo.setInsertUserId(entity.getTransferPriceUserId());
//					settleInfo.setSettletype(3);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//待办收费
//			List<TransferFinanceInfo> dbList = finance.getFinaneInfoByPeriodDB(view.getStartDate(),view.getEndDate(),Integer.parseInt(insertUserId));
//			if(dbList!=null)
//			{
//				for(int i=0;i<dbList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)dbList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(settleID));
//					settleInfo.setSettleUserId(Integer.parseInt(insertUserId));
//					settleInfo.setReceivable(200*entity.getCount());
//					settleInfo.setPaid(entity.getAgencyPrice());
//					settleInfo.setInsertUserId(entity.getAgencyPriceUserId());
//					settleInfo.setSettletype(6);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//待办超期/过期收费
//			List<TransferFinanceInfo> dbOutList = finance.getFinaneInfoByPeriodDBOut(view.getStartDate(),view.getEndDate(),Integer.parseInt(insertUserId));
//			if(dbOutList!=null)
//			{
//				for(int i=0;i<dbOutList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)dbOutList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(settleID));
//					settleInfo.setSettleUserId(Integer.parseInt(insertUserId));
//					settleInfo.setReceivable(100*entity.getCount());
//					settleInfo.setPaid(entity.getExtendedPrice());
//					settleInfo.setInsertUserId(entity.getExtendedPriceUserId());
//					settleInfo.setSettletype(7);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//变更交易票收费
//			List<TransferFinanceInfo> bgList = finance.getFinaneInfoByPeriodBG(view.getStartDate(),view.getEndDate(),Integer.parseInt(insertUserId));
//			if(bgList!=null)
//			{
//				for(int i=0;i<bgList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)bgList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(settleID));
//					settleInfo.setSettleUserId(Integer.parseInt(insertUserId));
//					settleInfo.setReceivable(200*entity.getCount());
//					settleInfo.setPaid(entity.getChangePrice());
//					settleInfo.setInsertUserId(entity.getChangePriceUserId());
//					settleInfo.setSettletype(5);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//流程表退费
//			List<TransferFinanceInfo> lcTList = finance.getFinaneInfoByPeriodLCT(view.getStartDate(),view.getEndDate(),Integer.parseInt(insertUserId));
//			if(lcTList!=null)
//			{
//				for(int i=0;i<lcTList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)lcTList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(settleID));
//					settleInfo.setSettleUserId(Integer.parseInt(insertUserId));
//					settleInfo.setReceivable(50*entity.getCount());
//					settleInfo.setPaid(entity.getCheckBackPrice());
//					settleInfo.setInsertUserId(entity.getCheckBackPriceUserId());
//					settleInfo.setSettletype(2);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
//			//交易票退费
//			List<TransferFinanceInfo> jypTList = finance.getFinaneInfoByPeriodJYPT(view.getStartDate(),view.getEndDate(),Integer.parseInt(insertUserId));
//			if(jypTList!=null)
//			{
//				for(int i=0;i<jypTList.size();i++)
//				{
//					TransferFinanceInfo entity = (TransferFinanceInfo)jypTList.get(i);
//					TransferSettlementInfo settleInfo = new TransferSettlementInfo();
//					settleInfo.setSettleId(Integer.parseInt(settleID));
//					settleInfo.setSettleUserId(Integer.parseInt(insertUserId));
//					settleInfo.setReceivable(200*(entity.getCount()));
//					if(entity.getOtherBackPrice()>0)
//					{
//						settleInfo.setPaid(entity.getOtherBackPrice());
//					}
//					if(entity.getSdBackPrice()>0)
//					{
//						settleInfo.setPaid(entity.getSdBackPrice());
//					}
//					if(entity.getPzBackPrice()>0)
//					{
//						settleInfo.setPaid(entity.getPzBackPrice());
//					}				
//					settleInfo.setInsertUserId(entity.getBackPriceUserId());
//					settleInfo.setSettletype(4);
//					settleInfo.insertSettleInfo(settleInfo);
//				}
//			}
			
			
			TmsTransferFinanceDetailDao dao = (TmsTransferFinanceDetailDaoImp) TmsDao.getInstance(TmsDao.TMS_TRANSFER_FINANCE_DETAIL);
			List<TransferSettlementInfo> list = dao.getFinaneInfoByPeriod(view.getStartDate(),view.getEndDate(), Integer.parseInt(insertUserId), Integer.parseInt(settleID), Codes.CHARGE_TYPE_CHARGE);
			for (TransferSettlementInfo transferSettlementInfo : list) transferSettlementInfo.insertSettleInfo(transferSettlementInfo);
			controller.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");

	}

}